package obj;

import game.Tri;
import game.Vertex;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ObjLoader {

	public List<Vertex> verts = new ArrayList<Vertex>();
	//public Map<Integer, List<Vertex> > indexVertexMapping = new HashMap<Integer, List<Vertex> >();
	//public Map<Integer, Vertex> indexVertexMapping = new HashMap<Integer, Vertex>();
	
	//public List<Tri> tris = new ArrayList<Tri>();
	//public Map<Integer, List<Tri> > indexTriMapping = new HashMap<Integer, List<Tri> >();
	
	public int triId = 0;
	
	public int vertexId = 0;
	
	public int verticesRead = 0;
	
	public Obj obj;

	public void load(String filename) throws IOException {

		BufferedReader br = new BufferedReader(new FileReader(filename));
		
		String line;
		
		while ((line = br.readLine()) != null) {
			
			if (line.startsWith("#")) {
				continue;
			}
			
			if (0 == line.length()) {
				continue;
			}
			
			if (line.matches("\\s+")) {
				continue;
			}
			
			if (line.startsWith("vt")) {
				processVertTextCoord(line);
			} else if (line.startsWith("vn")) {
				processVertNormal(line);
			} else if (line.startsWith("vp")) {
				processParameterSpace(line);
			} else if (line.startsWith("v")) {
				processVert(line);
			} else if (line.startsWith("f")) {
				processFace(line);
			} else if (line.startsWith("g")) {
				processPolygonGroup(line);
			} else if (line.startsWith("o")) {
				processObject(line);
			} else if (line.startsWith("s")) {
				processObject(line);
			}
			
			//System.out.println(line);
		   
		}
		
		br.close();
		
		cleanUp();
		
	}

	private void cleanUp() {
		verts = new ArrayList<Vertex>();
		
		triId = 0;
		vertexId = 0;
		verticesRead = 0;
		
		obj = null;
	}

	private void processVertTextCoord(String line) {
		// TODO Auto-generated method stub
		
	}

	private void processParameterSpace(String line) {
		// TODO Auto-generated method stub
		
	}

	private void processObject(String line) {
		// TODO Auto-generated method stub
		
	}

	private void processPolygonGroup(String line) {
		// TODO Auto-generated method stub
		
	}

	private void processFace(String line) {
		
		Tri tri = new Tri();
		tri.id = triId;
		triId++;
		
		obj.tris.add(tri);
		
		String parts[] = line.split("\\s+");
		
		if (4 != parts.length) {
			
			String msg = "Model contains faces that are not triangles!";
			System.out.println(msg);
			throw new RuntimeException(msg);
			
		}
		
		for (int i = 0; i < 3; i++) {
			
			String subParts[] = parts[i + 1].split("/");
			
			int tempVertexId = Integer.parseInt(subParts[0]);
			//tempVertexId -= 1;
			
			//tri.vertices[i] = verts.get(tempVertexId);
			tri.vertices[i] = obj.indexVertexMapping.get(tempVertexId);
			
			// vertices know their tris
			obj.indexVertexMapping.get(tempVertexId).tris.add(tri);
			
			//System.out.println(line);
			
		}
		
	}

	private void processVertNormal(String line) {
		// TODO Auto-generated method stub
		
	}

	private void processVert(String line) {
		
		verticesRead++;
		
		// parse the line into a vertex
		Vertex vertex = new Vertex();
		String parts[] = line.split("\\s+");
		
		vertex.x = Float.parseFloat(parts[1]);
		vertex.y = Float.parseFloat(parts[2]);
		vertex.z = Float.parseFloat(parts[3]);
		
		for (Vertex existingVertex : verts) {
			
			if (existingVertex.equals(vertex)) {
				
				//System.out.println("vertex \"" + line + "\" equals existing vertex! Reusing vertex: " + existingVertex.id);
				
				// add a mapping from that index to one single vertex and reuse that single
				// vertex whenever one of its indexes is used by a tri
				//
				// add a new alias
				obj.indexVertexMapping.put(verticesRead, existingVertex);				
				
				return;
			}
			
		}
		
		
		vertex.id = vertexId;
		vertexId++;
		
		obj.indexVertexMapping.put(verticesRead, vertex);
		verts.add(vertex);
		
	}

}
